﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for Funtion
/// </summary>
public class Funtion
{
    DataClassesDataContext dtc ;
	public Funtion()
	{
		//
		// TODO: Add constructor logic here
		//
       this.dtc = new DataClassesDataContext();
	}
    #region About
    public DateTime getdate(string loc)
    {
     tblMallAboutUs d = (from t in this.dtc.tblMallAboutUs
                    where t.UpdateDay == (this.dtc.tblMallAboutUs.Max(t1 => t1.UpdateDay)) && t.Location== loc
                    select t).First();
        return DateTime.Parse(d.UpdateDay.ToString()).Date;

    }
    public tblMallAboutUs LoadNews(string loc) 
    {
        tblMallAboutUs m = (from a in this.dtc.tblMallAboutUs
                where a.UpdateDay == getdate(loc) && a.Location==loc
                select a).First();
        return m;
    }
    public string getImage(string loc,string lc)
    {
        tblPhotosAUM i = (from m in this.dtc.tblPhotosAUMs
                     where m.AUMID == LoadNews(loc).AUMID && m.Location == lc
                     select m).First();
        return i.PhotoName.ToString();
    }
    #endregion
    #region Shop
    public List<tblShop> LoadShop()
    {
        var x = from i in this.dtc.tblShops
                orderby i.ShopID ascending
                select i;
        return x.ToList();
    }
    public List<tblShop> LoadShopByName(string name)
    {
        var x = from i in this.dtc.tblShops
                where i.ShopName.Contains(name)
                orderby i.ShopID ascending
                select i;
        return x.ToList();
    }
    public string GetShopID()
    {
        tblShop d = (from t in this.dtc.tblShops
                       where t.Stt == this.dtc.tblShops.Max(t1 => t1.Stt)
                       select t).First();
        int id = d.Stt + 1;
        return "SH0" + id;

    }
    public string GetShopDID()
    {
        tblShopDetail d = (from t in this.dtc.tblShopDetails
                     where t.Stt == this.dtc.tblShopDetails.Max(t1 => t1.Stt)
                     select t).First();
        int id = d.Stt + 1;
        return "SHD0" + id;

    }
    public tblShopDetail GetShopDetailID(string shid)
    {
        tblShopDetail d = (from t in this.dtc.tblShopDetails
                           where t.ShopID.Equals(shid)
                           select t).First();
        return d;
    }
    public string GetShopID1(string useid)
    {
        tblShop d = (from t in this.dtc.tblShops
                           where t.UseShID.Equals(useid)
                           select t).First();
        return d.ShopID;
    }
    public bool AddShop(string shid, string useid,string loc,DateTime start,DateTime end,string notes)
    {
        try
        {
            tblShop sh = new tblShop();
            sh.ShopID = shid;
            sh.UseShID = useid;
            sh.ShopName = "";
            sh.Location = loc;
            sh.StartDate = start.Date;
            sh.EndDate = end.Date;
            sh.Notes = notes;
            this.dtc.tblShops.InsertOnSubmit(sh);
            this.dtc.SubmitChanges();
        }
        catch (Exception)
        {
            return false;
        }
        return true;

    }
    public bool AddShopDetail(string shiddt, string shid,string loc)
    {
        try
        {
            tblShopDetail sh = new tblShopDetail();
            sh.ShopDID = shiddt;
            sh.ShopID = shid;
            sh.ShopName ="";
            sh.AboutUs = "";
            sh.Location = loc;
            sh.Notes = "";
            this.dtc.tblShopDetails.InsertOnSubmit(sh);
            this.dtc.SubmitChanges();
        }
        catch (Exception)
        {
            return false;
        }
        return true;

    }
    public bool UpdateShop(string shid, DateTime start, DateTime end, string notes)
    {
        try
        {
            tblShop sh = (from i in this.dtc.tblShops where i.ShopID == shid select i).Single();
            sh.StartDate = start.Date;
            sh.EndDate = end.Date;
            sh.Notes = notes;
            this.dtc.SubmitChanges();
        }
        catch (Exception)
        {
            return false;
        }
        return true;

    }
    public bool UpdateShop2(string shid,string shname)
    {
        try
        {
            tblShop sh = (from i in this.dtc.tblShops where i.ShopID == shid select i).Single();
            sh.ShopName = shname;
            this.dtc.SubmitChanges();
        }
        catch (Exception)
        {
            return false;
        }
        return true;

    }
    public bool UpdateShopDetail(string shid, string shame, string about,string notes)
    {
        try
        {
            tblShopDetail sh = (from i in this.dtc.tblShopDetails where i.ShopID == shid select i).Single();
            sh.ShopName = shame;
            sh.AboutUs = about;
            sh.Notes = notes;
            this.dtc.SubmitChanges();
        }
        catch (Exception)
        {
            return false;
        }
        return true;

    }
    #endregion
}